/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.util.ArrayList;
import java.util.List;
import model.*;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Franz
 */
public class KpersonHandler {
    private Session session;
    public KpersonHandler(Session session){
        this.session = session;
    }
    
    
   public KpersonFil[] getKpersonFil(Kontaktperson kperson){
        Object[] obArr = kperson.getKpersonFils().toArray();
        
       KpersonFil[] elArr = new KpersonFil[obArr.length];
       
        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (KpersonFil) obArr[i];
        }
        
        return elArr;  
    }
   
   
    public KpersonTelefon[] getKpersonTelefon(Kontaktperson kperson){
        Object[] obArr = kperson.getKpersonTelefons().toArray();
       KpersonTelefon[] koArr = new KpersonTelefon[obArr.length];
       
        for (int i = 0; i < obArr.length; i++) {
            koArr[i] = (KpersonTelefon) obArr[i];
        }
        
        return koArr;  
    }
    
    public void OpretKpersonFil( KpersonFil kpf){
   
        session.beginTransaction();
        String hql = "from Kontaktperson";
        session.flush();
        kpf.getKontaktperson().getKpersonFils().add(kpf);
        session.createQuery(hql);
        session.save(kpf);
        session.getTransaction().commit();
        
        
        
    }
    
    public void OpretKpersonTelefon(KpersonTelefon kpt){
        
        
        session.beginTransaction();
        String hql = "from Kontaktperson";
        session.flush();
        kpt.getKontaktperson().getKpersonTelefons().add(kpt);
        session.createQuery(hql);
        session.save(kpt);
        session.getTransaction().commit();
        
    }
    
      public void sletKpersonTelefon(KpersonTelefon tlf) {
        session.beginTransaction();
        String hql = "from KpersonTelefon";
        session.flush();
        session.createQuery(hql);
        session.delete(tlf);
           tlf.getKontaktperson().getKpersonTelefons().remove(tlf);
    }

    public void updatekperson(Kontaktperson kperson) {
        

        session.beginTransaction();
        String hql = "from Kontaktperson";
        session.createQuery(hql);
 
        session.flush();
        session.update(kperson);
        session.getTransaction().commit();

    
    }

    public void sletKpersonFil(KpersonFil fil) {
        session.beginTransaction();
        String hql = "from KpersonFil";
        session.flush();
        session.createQuery(hql);
        session.delete(fil);
        fil.getKontaktperson().getKpersonFils().remove(fil);
    }

public Postnr hentPostnr(String pnr){
        
        String hql = "FROM Postnr where postNummer = \'" + pnr + "\'";
        Postnr p;
        session.beginTransaction();
        Query q = session.createQuery(hql);
         List resultList = q.list();
        if(resultList.size() > 0){
             p = (Postnr) resultList.get(0);
        }else{
            p = new Postnr("0000", "Ugyldigt Postnummer");
        }
        session.flush();
        session.getTransaction().commit();
        return p;
    }

    public void sletkPerson(Kontaktperson kperson) {
        session.beginTransaction();
        String hql = "from Kontaktperson";
        session.flush();
        Object[] kpf = kperson.getKpersonFils().toArray();

        for (int i = 0; i < kpf.length; i++) {
            sletKpersonFil((KpersonFil) kpf[i]);
        }
        Object[] kpt = kperson.getKpersonTelefons().toArray();
        for (int i = 0; i < kpt.length; i++) {
            sletKpersonTelefon((KpersonTelefon)kpt[i]);
        }
        Object[] elevs = kperson.getElevs().toArray();
        for (int i = 0; i < elevs.length; i++) {
            Elev el = (Elev) elevs[i];
            el.getKontaktpersons().remove(kperson); 
        }
        session.createQuery(hql);
        session.delete(kperson);
        session.getTransaction().commit();
    }

    public void nyKperson(Kontaktperson kperson) {
         session.beginTransaction();
        String hql = "from Kontaktperson";
        session.createQuery(hql);
        session.save(kperson);
        session.flush();
        session.getTransaction().commit();

    }
    
   
   
    
}
